package com.danco.training.zaneuskaya.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Account;
import com.danco.training.zaneuskaya.repository.AccountDao;
import com.danco.training.zaneuskaya.service.AccountService;
import com.danco.training.zaneuskaya.service.exception.UserException;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

	private static final String ACCOUNT_DOESN_T_EXIST = "Account doesn't exist";

	private static final String EMAIL_IS_USED = "Email is used";

	private static final String ACCOUNT_IS_LOGGED_OUT = "Account is logged out";

	private static final Logger logger = Logger
			.getLogger(AccountServiceImpl.class);

	@Autowired
	private AccountDao accountDao;

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public void createEntity(Account t) throws UserException {
		try {
			if (accountDao.findAccountByLogin(t.getEmail()) == null) {
				accountDao.create(t);
			} else {
				throw new UserException(EMAIL_IS_USED);
			}

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		}catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		} 
	}

	public void deleteEntityById(Integer id) {
		try {
			Account account = accountDao.read(id);
			
				accountDao.delete(account);
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	public void updateEntity(Account t) {
		try {
			Account account = accountDao.read(t.getAccountId());
			account.setAccountRole(t.getAccountRole());
			account.setEmail(t.getEmail());
			account.setPassword(t.getPassword());
			accountDao.update(account);
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);

		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	public Account getEntityById(Integer id) {
		Account account = null;
		try {
			account = accountDao.read(id);
			if (account == null) {
				throw new NullPointerException(ACCOUNT_DOESN_T_EXIST);
			}

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);

		}
		
		catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return account;
	}

	public List<Account> getEntityList() {
		List<Account> accounts = new ArrayList<Account>();
		try {
			accounts = accountDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		}catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return accounts;
	}

	public Account getAccountByEmail(String email) throws UserException {
		Account account = null;
		try {
			account = accountDao.findAccountByLogin(email);
			if (account == null) {
				throw new UserException(ACCOUNT_DOESN_T_EXIST);
			}

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		}catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return account;
	}

	public Account getAccountByEmailPassword(String email, String password)
			throws UserException {
		Account account = null;
		try {
			account = accountDao.login(email, password);
			if (account == null) {
				throw new UserException(ACCOUNT_DOESN_T_EXIST);
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		}catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return account;
	}

	@Override
	public void logout() {
		logger.info(ACCOUNT_IS_LOGGED_OUT);
	}

}
